package com.cskaoyan.market.service.admin;

import com.cskaoyan.market.db.domain.MarketLog;
import com.cskaoyan.market.db.domain.MarketLogExample;
import com.cskaoyan.market.db.mapper.MarketLogMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service

public class MarketLogServiceImpl implements MarketLogService {
   @Autowired
    MarketLogMapper logMapper;
    @Override
    public List<MarketLog> list(Integer page, Integer limit, String sort, String order,String name) {
        MarketLogExample marketLogExample = new MarketLogExample();
        MarketLogExample.Criteria criteria = marketLogExample.createCriteria();
        marketLogExample.setOrderByClause(sort+" "+order);
        if(!StringUtils.isEmpty(name)){
            criteria.andAdminEqualTo(name);
        }
        PageHelper.startPage(page,limit);
        List<MarketLog> marketLogList = logMapper.selectByExampleSelective(marketLogExample, MarketLog.Column.id, MarketLog.Column.admin,
                MarketLog.Column.ip, MarketLog.Column.type, MarketLog.Column.action, MarketLog.Column.status, MarketLog.Column.result,
                MarketLog.Column.comment, MarketLog.Column.addTime, MarketLog.Column.updateTime, MarketLog.Column.deleted);
        return marketLogList;
    }

    @Override
    public boolean addLog(String adminName, String ip, int type, String action, boolean status, String optionResult, String comment, LocalDateTime now, LocalDateTime now1, boolean deleted) {

        try {
            MarketLog marketLog = new MarketLog();
            marketLog.setDeleted(deleted);
            marketLog.setAdmin(adminName);
            marketLog.setIp(ip);
            marketLog.setType(Integer.valueOf(type));
            marketLog.setAction(action);
            marketLog.setStatus(status);
            marketLog.setResult(optionResult);
            marketLog.setComment(comment);
            marketLog.setAddTime(now);
            marketLog.setUpdateTime(now1);
            logMapper.insert(marketLog);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
